package com.binvshe.binvshe.organization;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.Account;
import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.ui.LoginActivity;
import com.binvshe.binvshe.app.MainActivity;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.common.huanxin_im.ChatHelper;
import com.binvshe.binvshe.common.image.IML;
import com.binvshe.binvshe.mine.corporation.ClubSettingActivity;
import com.binvshe.binvshe.mine.view.CommentDialog;
import com.binvshe.binvshe.organization.entity.ClubEntity;
import com.binvshe.binvshe.organization.entity.ClubUserEntity;
import com.binvshe.binvshe.organization.entity.OpusListEntity;
import com.binvshe.binvshe.release.ContentType;
import com.binvshe.binvshe.release.ReleaseImageContentActivity;
import com.bumptech.glide.Glide;
import com.cncn.youmengsharelib.ShareData;
import com.cncn.youmengsharelib.ShareListener;
import com.cncn.youmengsharelib.SharePlatform;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.nineoldandroids.view.ViewHelper;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.baseuicomponent.view.CenterToolbar;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.share.BaseShareFragment;
import com.pachong.android.framework.share.SharePlatformsFragment;
import com.pachong.android.frameworkbase.dialog.IOSAlertListFragment;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.ColorFilterTransformation;

import static com.binvshe.binvshe.organization.OpusAdapter.JBulderType.SHARE;
import static com.binvshe.binvshe.organization.OpusFragment.IdentityStatus.VISITOR;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/13
 * 社团首页作品展示页
 */
public class OpusFragment extends SwipeListFragment implements ObservableScrollViewCallbacks, View.OnClickListener {
    private static final String KEY_CLUBID = "CLUBID";
    private static final int shareReqCode = 812;

    /**
     * 头像下面按钮状态
     */
    enum SignBtnStatus {
        SIGNIN,//签到
        SIGNDAYS,//签到多少天
        ATTENTION,//关注
        ATTENTION_ALREADY//已关注
    }

    enum FloatingBtnStatus {
        RELEASE,//发布
        APPLYFor //申请入团
    }

    enum IdentityStatus {//身份
        MEMBER,//成员
        ADMIN,//管理员
        OTHER,//外人
        VISITOR,//游客
    }

    @InjectExtra(KEY_CLUBID)
    int clubID;
    int mLeaderID = -1;
    CircleImageView civHead;
    boolean isHide = false;

    ListDataRequestListener<OpusListEntity> mRequestListener = new ListDataRequestListener(this, OpusListEntity.class);
    int mPage = 0;
    @Bind(R.id.obsRecyclerview)
    ObservableRecyclerView mObsRecyclerview;
    @Bind(R.id.btnFloating)
    Button mBtnFloating;
    @Bind(R.id.swlyt_swip)
    SwipeRefreshLayout mSwlytSwip;
    @Bind(R.id.btnSendMessage)
    Button mBtnSendMessage;
    private ImageView viewHeader;
    private TextView tvClubName;
    private TextView tvClubPlace;
    private TextView tvFansNum;
    private TextView tvClubID;
    private Button btnSignIn;
    private TextView tvClubDesc;
    private LinearLayout lilytMember;
    private RelativeLayout relytViewHeader;
    private TextView mTvNotice;
    private ImageView iv_back;
    private View mHeaderView;
    SignBtnStatus mSignBtnStatus;
    FloatingBtnStatus mFloatingBtnStatus;
    IdentityStatus mUserIdentity;
    boolean isMyClub;
    ShareData mShareData = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        mHeaderView = inflater.inflate(R.layout.view_orglist_header, null, false);
        this.relytViewHeader = (RelativeLayout) mHeaderView.findViewById(R.id.relytViewHeader);
        this.lilytMember = (LinearLayout) mHeaderView.findViewById(R.id.lilytMember);
        this.tvClubDesc = (TextView) mHeaderView.findViewById(R.id.tvClubDesc);
        this.btnSignIn = (Button) mHeaderView.findViewById(R.id.btnAttention);
        this.tvClubID = (TextView) mHeaderView.findViewById(R.id.tvClubID);
        this.tvFansNum = (TextView) mHeaderView.findViewById(R.id.tvFansNum);
        this.tvClubPlace = (TextView) mHeaderView.findViewById(R.id.tvClubPlace);
        this.tvClubName = (TextView) mHeaderView.findViewById(R.id.tvClubName);
        this.viewHeader = (ImageView) mHeaderView.findViewById(R.id.viewHeader);
        this.mTvNotice = (TextView) mHeaderView.findViewById(R.id.tvClubNotice);
        civHead = (CircleImageView) mHeaderView.findViewById(R.id.civHead);
//        this.iv_back = (ImageView) mHeaderView.findViewById(R.id.iv_back);

        getAdapter().setHeaderView(mHeaderView);
        btnSignIn.setOnClickListener(this);
        lilytMember.setOnClickListener(this);
//        iv_back.setOnClickListener(this);
        initClubState();
        initTitle();
        //判断当前是否加入社团
        return rootView;
    }


    @Override
    public void onResume() {
        super.onResume();
        getClubDetail();
        getClubMembers();
        onStartLoading();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 初始化标题栏
     */
    private void initTitle() {
        boolean showMyToolBar = getResources().getBoolean(R.bool.opus_fragment_custom_toolbar);
        CenterToolbar toolbar;
        if (showMyToolBar) {
            toolbar = myToolbar;
            toolbar.setBackgroundColor(Color.TRANSPARENT);

        } else {
            showToolbar();
            toolbar = getCustomToolbar();
            toolbar.setBackgroundColor(Color.parseColor("#38c5ff"));
            SystemBarTintManager manager = new SystemBarTintManager(getActivity());
            manager.setStatusBarTintColor(Color.parseColor("#38c5ff"));
        }

        toolbar.setVisibility(View.VISIBLE);
        toolbar.enableBackButton(getResources().getDrawable(R.mipmap.share_nav_button_back_default_w));
        toolbar.addRightImageButton(R.mipmap.icon_moerssss, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> list = new ArrayList<String>();
                IOSAlertListFragment iosAlertListFragment = new IOSAlertListFragment();
                AdapterView.OnItemClickListener onItemClickListener = null;
                switch (mUserIdentity) {
                    case ADMIN:
                        list.add("分享");
                        list.add("社团管理");
                        onItemClickListener = new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                switch (position) {
                                    case 0:
                                        share();
                                        break;
                                    case 1:
                                        ClubSettingActivity.start(getContext());
                                        break;
                                }
                            }
                        };
                        break;
                    case MEMBER:
                        list.add("分享");
                        list.add("退出社团");
                        onItemClickListener = new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                switch (position) {
                                    case 0:
                                        share();
                                        break;
                                    //退出社团
                                    case 1:
                                        setExitClub();
                                        break;
                                }
                            }
                        };
                        break;
                    case OTHER:
                    case VISITOR:
                    default:
                        list.add("分享");
                        onItemClickListener = new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                share();
                            }
                        };
                        break;
                }
                iosAlertListFragment.setActionList(onItemClickListener, list);
                iosAlertListFragment.show(getFragmentManager());
            }
        });
    }

    public static OpusFragment newInstance(int clubID) {
        Bundle args = new Bundle();
        args.putInt(KEY_CLUBID, clubID);
        OpusFragment fragment = new OpusFragment();
        fragment.setArguments(args);
        return fragment;
    }


    //判断身份状态
    private void initClubState() {
        if (AccountManager.isTokenValid()) {// 登录状态 1.判断是否有社团 2.判断自己是否属于该社团
            Account account = AccountManager.getCurrentAccount();
            if (account.getInClub()) {//有社团
                if (account.getClub().getId() == clubID) {//clubID 相同，我的社团
                    setMyClub(true);
                    return;
                } else {//有社团，但不属于该社团
                    setUserIdentity(IdentityStatus.OTHER);
                }
            } else {//没有社团
                setUserIdentity(IdentityStatus.OTHER);
            }
        } else {//token无效，游客模式
            setMyClub(false);
            setUserIdentity(VISITOR);
            return;
        }
        setMyClub(false);
    }

    //设置view 的显示
    public void setMyClub(boolean myClub) {
        isMyClub = myClub;
        Account account = AccountManager.getCurrentAccount();
        if (myClub) {
            btnSignIn.setText("签到");
            mBtnFloating.setText("发布");
            mBtnSendMessage.setText("加入群聊");
            mTvNotice.setVisibility(View.VISIBLE);
            setFloatingBtnStatus(FloatingBtnStatus.RELEASE);
        } else {
            btnSignIn.setText("关注");
            mBtnFloating.setText("申请加入");
            mBtnSendMessage.setVisibility(View.GONE);
            if(account.getInClub()){
                mBtnFloating.setVisibility(View.GONE);
            }
            setFloatingBtnStatus(FloatingBtnStatus.APPLYFor);

        }
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        mPage = 0;
        getData().clear();
        onStartLoading();
    }


    /**
     * 设置头部view的数据
     *
     * @param clubInfo
     */
    public void setHeaderViewData(ClubUserEntity clubInfo) {
        ClubEntity clubEntity = clubInfo.getClub();
        Log.e("服务器返回的clubEntity",clubEntity.toString());
        boolean in_club = clubInfo.isIn_club();
        Log.e("in_club",""+in_club);
        IML.load(getContext(), civHead, clubEntity.getPortrait());
        Glide.with(getContext())
                .load(clubEntity.getPortrait())
                .crossFade(1000)
                .bitmapTransform(new BlurTransformation(getContext(), 23, 4),// “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                        new ColorFilterTransformation(getContext(), Color.argb(60, 96, 96, 96)))
                .into(viewHeader);
        tvClubName.setText(clubEntity.getName());
        tvClubPlace.setText(clubEntity.getCity());
        tvFansNum.setText(getString(R.string.fansNum, clubEntity.getFans() + ""));
        tvClubID.setText(getString(R.string.idNum, clubEntity.getId() + ""));
        tvClubDesc.setText(clubEntity.getSign());
        mLeaderID = clubEntity.getLeaderID();

        mTvNotice.setText(TextUtils.isEmpty(clubEntity.getNote()) ? "暂无公告" : clubEntity.getNote());

        if (isMyClub) {//是我的社团
            if (clubInfo.isCheck_in()) {//是否已签到
                int days = clubInfo.getCheck_in_days();
                btnSignIn.setText(getString(R.string.signDays, days + ""));
                setSignBtnStatus(SignBtnStatus.SIGNDAYS);
                Log.e("我的社团","我的社团");
            } else {//未签到
                setSignBtnStatus(SignBtnStatus.SIGNIN);
                Log.e("未签到","未签到");
            }
            if (TextUtils.equals(clubEntity.getLeaderID() + "", AccountManager.getCurrentAccount().getUserId())) {//是管理员
                setUserIdentity(IdentityStatus.ADMIN);
            } else {//非管理员
                setUserIdentity(IdentityStatus.MEMBER);
            }
        } else {//不是我的社团
            if (clubInfo.isFollow()) {//是否已关注
                setSignBtnStatus(SignBtnStatus.ATTENTION_ALREADY);
            } else {//未关注
                setSignBtnStatus(SignBtnStatus.ATTENTION);
            }
        }
    }


    @Bind(R.id.myToolbar)
    public CenterToolbar myToolbar;

    @Override
    public View createRecyclerContentView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.observable_recyclerview, null);
        ButterKnife.bind(this, view);
        Dart.inject(this, getArguments());
        setRecyclerView(mObsRecyclerview);
        mSwipeRefreshViewHelper.initRefreshView(mSwlytSwip, this);
        mObsRecyclerview.setScrollViewCallbacks(this);
        return view;
    }


    public void setSignBtnStatus(SignBtnStatus signBtnStatus) {
        mSignBtnStatus = signBtnStatus;
        switch (signBtnStatus) {
            case SIGNIN:
                btnSignIn.setText("签到");
                break;
            case ATTENTION:
                btnSignIn.setText("关注");
                break;
            case ATTENTION_ALREADY:
                btnSignIn.setText("已关注");
                break;
        }
    }

    public void setFloatingBtnStatus(FloatingBtnStatus floatingBtnStatus) {
        mFloatingBtnStatus = floatingBtnStatus;
    }

    /**
     * 设置用户身份
     *
     * @param userIdentity
     */
    public void setUserIdentity(IdentityStatus userIdentity) {
        mUserIdentity = userIdentity;
        switch (mUserIdentity) {
            case ADMIN:

                ((OpusAdapter) getAdapter()).setJBulderType(OpusAdapter.JBulderType.SHARE);
//                ((OpusAdapter) getAdapter()).setJBulderType(OpusAdapter.JBulderType.DELETE);
                break;
            case VISITOR:
                mBtnFloating.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LoginActivity.start(getActivity());
                    }
                });

                break;
        }
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new OpusAdapter(getActivity());
    }

    @Override
    public void onStartLoading() {
        getOpusList();
    }


    /**
     * 退出社团
     */
    public void setExitClub() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.EXIT_CLUB, clubID);
        request.delete(url, null, new DataRequestListener<String>(String.class) {
            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                AccountManager.getCurrentAccount().setInClub(false);
                AccountManager.getCurrentAccount().setClub(null);
                EasyToast.showToast(getContext(), "退出成功");
                startActivity(new Intent(getActivity(), MainActivity.class));
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(getContext(), strMsg);
            }
        });
    }

    /**
     * 获取社团成员
     */
    public void getClubMembers() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.GET_CLUB_MEMBER, clubID);
        UrlParams params = new UrlParams();
        params.put("page", 1 + "");
        params.put("size", 6 + "");
        request.get(url, params, new DataRequestListener<ArrayList<Account>>(Account.class) {
            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(ArrayList<Account> o) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.circleSmall), getResources().getDimensionPixelOffset(R.dimen.circleSmall));
                layoutParams.setMargins(10, 0, 0, 0);
                int childnum = lilytMember.getChildCount();
                lilytMember.removeViews(1, childnum - 1);
                if (o != null) {
                    for (Account account : o) {
                        CircleImageView circleImageView = new CircleImageView(getContext());
                        circleImageView.setLayoutParams(layoutParams);
                        IML.load(getContext(), circleImageView, account.getPortrait());
                        lilytMember.addView(circleImageView);
                    }
                    int count = getResponseDataParser().getDataJsonObj().optInt("total_count");
                    if (count > 6) {
                        TextView tvMemberNum = new TextView(getContext());
                        tvMemberNum.setGravity(Gravity.CENTER);
                        tvMemberNum.setTextSize(12);
                        tvMemberNum.setLayoutParams(layoutParams);
                        tvMemberNum.setBackgroundResource(R.drawable.circle_text);
                        if (count < 100) {
                            tvMemberNum.setText(count);
                        } else {
                            tvMemberNum.setText("99+");
                        }
                        lilytMember.addView(tvMemberNum);
                    }
                }
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    /**
     * 获取列表数据
     */
    public void getOpusList() {
        // 第一次请求 || 还有下一页
        if (mPage == 0 || mRequestListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new MyHttpRequest(getContext());
            UrlParams params = new UrlParams();
            params.put("page", mPage + "");
            params.put("size", 6 + "");
            params.put("club_id", clubID + "");
            request.get(UrlCenter.GET_OPUS, params, mRequestListener);
        }

    }


    /**
     * 关注社团(接口)
     */
    private void addAttentionClub() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.ADD_CLUB_ATTENTION, clubID + "");
        UrlParams params = new UrlParams();
        request.post(url, params, new DataRequestListener<String>(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(String o) {
                setSignBtnStatus(SignBtnStatus.ATTENTION_ALREADY);
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }


    /**
     * 取消关注
     */
    private void deleteAttentionClub() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.DEL_CLUB, clubID + "");
        UrlParams params = new UrlParams();
        request.delete(url, params, new DataRequestListener<String>(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(String o) {
                setSignBtnStatus(SignBtnStatus.ATTENTION);
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    /**
     * 签到（接口）
     */
    private void addSign() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.POST_CLUB_CHECKIN, clubID + "");
        request.post(url, new UrlParams(), new DataRequestListener<String>(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(String o) {
                getClubDetail();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(getContext(), strMsg, Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }


    ClubEntity clubEntity;

    /**
     * 获取社团详情（含用户信息）
     */
    private void getClubDetail() {
        if (clubID == -1) {
            Toast.makeText(getContext(), "请求错误", Toast.LENGTH_SHORT).show();
            return;
        }
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.GET_CLUB_DETATLS_STATE, clubID);
        request.get(url, null, new DataRequestListener<ClubUserEntity>(ClubUserEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(ClubUserEntity o) {
                setHeaderViewData(o);
                clubEntity = o.getClub();
                initShareData(o);
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    private void initShareData(ClubUserEntity o) {
        ClubEntity club = o.getClub();
        boolean in_club = o.isIn_club();

        String url = "http://app.binvshe.com/static/app/index.html#/download";
        mShareData = new ShareData(club.getPortrait(), club.getSign(), club.getName(), url);
    }

    /**
     * 申请入团接口
     *
     * @param reason
     */
    private void applyJoinClub(String reason) {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.POST_APPLY_CLUB, clubID + "");
        JSONObject object = new JSONObject();
        try {
            object.put(ParamKey.APPLY_REASON, reason);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request.post(url, object, new DataRequestListener<String>(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(String o) {
                Toast.makeText(getContext(), "申请成功,请耐心等待审核", Toast.LENGTH_SHORT).show();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(getContext(), strMsg, Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    @Override
    public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
        setPullToRefreshEnable(scrollY == 0);

        if (scrollY < (getContext().getResources().getDimensionPixelOffset(R.dimen.headerHeight))) {
            ViewHelper.setTranslationY(mHeaderView, scrollY / 2);
        }
        //淡入淡出动画
        if (scrollY < (getContext().getResources().getDimensionPixelOffset(R.dimen.headerHeight) - 100)) {
            if (!isHide) {
                YoYo.with(Techniques.FadeIn)
                        .duration(200)
                        .playOn(relytViewHeader);
                isHide = !isHide;
            }
        } else {
            if (isHide) {
                YoYo.with(Techniques.FadeOutLeft)
                        .duration(200)
                        .playOn(relytViewHeader);
                isHide = !isHide;
            }
        }
    }

    @Override
    public void onDownMotionEvent() {

    }

    boolean isExit = true;

    @Override
    public void onUpOrCancelMotionEvent(ScrollState scrollState) {
        try {
            switch (scrollState) {
                case UP:
                    if (isExit) {
                        YoYo.with(Techniques.FadeOutDown)
                                .duration(200)
                                .playOn(mBtnFloating);
                    }
                    isExit = false;
                    break;
                case DOWN:
                    if (!isExit) {
                        YoYo.with(Techniques.FadeInDown)
                                .duration(200)
                                .playOn(mBtnFloating);

                    }
                    isExit = true;
                    break;
            }
        } catch (NullPointerException e) {

        }

    }

    @OnClick({R.id.btnFloating, R.id.btnSendMessage})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnFloating:
                switch (mFloatingBtnStatus) {
                    case APPLYFor:
                        CommentDialog dialog = CommentDialog.newInstance("申请入团", getString(R.string.hint_apply_reason), 5, true);
                        dialog.setOnClickable(new CommentDialog.CommentClickable() {
                            @Override
                            public void btnEnter(String comment) {
                                applyJoinClub(comment);
                            }
                        });
                        dialog.show(getFragmentManager(), "tag");
                        break;
                    case RELEASE:
//                        ClubReleaseActivity.start(getContext(), clubID + "");
                        ReleaseImageContentActivity.start(getActivity(), ContentType.IMAGE, "", clubID + "");
                        break;
                }
                break;
            case R.id.btnAttention:
                switch (mSignBtnStatus) {
                    case ATTENTION:
                        addAttentionClub();
                        break;
                    case ATTENTION_ALREADY:
                        deleteAttentionClub();
                        break;
                    case SIGNIN:
                        addSign();
                        break;
                    default:
                        break;
                }
                break;
            case R.id.btnSendMessage:
                if (clubEntity != null) {
                    if (!TextUtils.isEmpty(clubEntity.getIm_group_id())) {
                        ChatHelper.startGroupChat(getActivity(), clubEntity.getIm_group_id(), clubEntity.getName(), clubEntity.getPortrait());
                    }
                } else {
//                    throw new RuntimeException("还没有从服务端请求到群的id， 没办法聊天， 请找PV");
                    EasyToast.showToast(getActivity(), "正在获取社区消息，请等待");

                }
                break;
            case R.id.lilytMember:
                if (mLeaderID != -1) {
                    //成员管理转移到群设置
//                    ClubMemberActivity.start(getContext(), clubID, mLeaderID);
                }
                break;
//            case R.id.iv_back:
//                getActivity().finish();
//                break;
        }
    }


    private void share() {
        if (mShareData != null) {
            BaseShareFragment fragment = SharePlatformsFragment.showShareFragment(getActivity(), mShareData);
            fragment.setShareListener(new ShareListener() {
                @Override
                public void onShareSuccess(Activity shareActivity, SharePlatform platform) {
                    EasyToast.showToast(getActivity(), "分享成功");
                }

                @Override
                public void onShareFailed(Activity shareActivity, SharePlatform platform) {
                    EasyToast.showToast(getActivity(), "分享失败");
                }

                @Override
                public void onShareCancel(Activity shareActivity, SharePlatform platform) {
                    EasyToast.showToast(getActivity(), "分享取消");
                }
            });
        }
    }
}
